package com.atguigu.gmall.list.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.list.service.ListService;
import com.atguigu.gmall.model.list.Goods;
import com.atguigu.gmall.model.list.SearchParam;
import com.atguigu.gmall.model.list.SearchResponseAttrVo;
import com.atguigu.gmall.model.list.SearchResponseVo;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.transport.TransportClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.web.bind.annotation.*;

/**
 * @Author: 赵勇镔
 * @Time: 2021/5/22 14:45
 * @Description:
 */

@RestController
@RequestMapping("/api/list")
public class ListApiController {
    //Spring官方封装  //ES 官方近期高级别的客户端
    @Autowired
    private ElasticsearchRestTemplate elasticsearchRestTemplate;

    //Spring官方封装  ES官方早期低级别的客户端
    //@Autowired
    //private ElasticsearchTemplate elasticsearchTemplate;

    //ES 官方近期高级别的客户端
    @Autowired
    private RestHighLevelClient restHighLevelClient;

    //ES官方早期低级别的客户端
    //@Autowired
    //private TransportClient transportClient;

    @Autowired
    private ListService listService;

    //创建索引 并Mapping映射
    @GetMapping("/createIndex")
    public Result createIndex() {
        //创建索引库
        elasticsearchRestTemplate.createIndex(Goods.class);

        //mapping映射
        elasticsearchRestTemplate.putMapping(Goods.class);
        return Result.ok();
    }

    //上架之导入数据
    @GetMapping("/onSale/{skuId}")
    public Result onSale(@PathVariable Long skuId) {
        listService.onSale(skuId);
        return Result.ok();
    }

    //下架之删除数据
    @GetMapping("/cancelSale/{skuId}")
    public Result cancelSale(@PathVariable Long skuId) {
        listService.cancelSale(skuId);
        return Result.ok();
    }

    //增加热度
    @GetMapping("/hotScore/{skuId}")
    public Result hotScore(@PathVariable Long skuId){
        listService.hotScore(skuId);
        return Result.ok();
    }

    //开始搜索
    @PostMapping
    public SearchResponseVo list(@RequestBody SearchParam searchParam){
        return listService.list(searchParam);
    }







}
